package cn.wit.summit.chwan.utils;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by pengsk on 2016/3/3.
 */
public class AESUtils {
    public static final String TAG = "AESUtils";

    //aes加密
    public static String encrypt4Comment(String data) {
        try {
            String key = "625202f9149e061d";
            final String ALGORITHM = "AES";
            final String ALGORITHM_STR = "AES/ECB/PKCS7Padding";

            Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
            SecretKeySpec sks = new SecretKeySpec(key.getBytes(), ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, sks);
            byte[] encrypted = cipher.doFinal(data.getBytes());

            return bytesToHexString(encrypted).toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //aes加密
    public static String keyEncrypt(String data) {
        try {
            String key = "625202f9149e061d";

            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            int blockSize = cipher.getBlockSize();

            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");

            cipher.init(Cipher.ENCRYPT_MODE, keyspec);
            byte[] encrypted = cipher.doFinal(plaintext);

            return bytesToHexString(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //aes解密
    public static String keyDecrypt(String data) {
        try {
            String key = "625202f9149e061d";

            byte[] encrypted1 = hexStringToBytes(data.toLowerCase());

            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");

            cipher.init(Cipher.DECRYPT_MODE, keyspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
}
